package com.hx.mylombok.task.impl;

import com.hx.mylombok.task.AbstractFileTask;
import com.hx.mylombok.task.FileTask;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @remark --
 * @author: GuoXiaolin
 * @date 2019/4/24 11:01
 * @Version 1.0
 * @Derction
 */
public class ImplServiceTask extends AbstractFileTask implements FileTask {

    public ImplServiceTask(String className) {
        super(className);
    }

    @Override
    public void generateFile(Map<String, String> setterMap, ProcessingEnvironment processingEnv) throws IOException {

        JavaFileObject builderControllerFile = processingEnv.getFiler().
                createSourceFile(packageName + ".service.impl.Service"+sufixName);

        try (PrintWriter out = new PrintWriter(builderControllerFile.openWriter())) {

            if (packageName != null) {
                out.print("package ");
                out.print(packageName);
                out.println(".service.impl;");
                out.println();
            }

            out.println("import com.hx.battery.common.annotation.EntityService;");
            out.println("import "+packageName+".domain."+sufixName+";");
            out.println("import "+packageName+".service.abs.AbstractService"+sufixName+";");
            out.println("import org.slf4j.Logger;");
            out.println("import org.slf4j.LoggerFactory;");
            out.println("import org.springframework.beans.factory.annotation.Autowired;");
            out.println("import org.springframework.stereotype.Service;");
            out.println("import org.springframework.transaction.annotation.Transactional;");
            out.println("import "+packageName+".repository.IRepository"+sufixName+";");
            out.println();

            out.println("@EntityService(" + sufixName + ".class)");
            out.println("@Transactional(readOnly = true)");
            out.println("@Service");
            out.println("public class Service" + sufixName + " extends AbstractService" + sufixName + "{");
            out.println();
            out.println("    private static final Logger logger = LoggerFactory.getLogger(Service"+sufixName+".class);");
            out.println("    private IRepository"+sufixName+" repository"+sufixName+";");
            out.println("    @Autowired");
            out.println("    public Service"+sufixName+"(IRepository"+sufixName+" repository"+sufixName+"){");
            out.println("    this.repository"+sufixName+" = repository"+sufixName+";");
            out.println("}");
            out.println();
            out.println("}");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            logger.info("ImplServiceTask of " + simpleClassName + " is over");
        }
    }
}
